<?php
namespace Book\Controller;
use Think\Controller;
class BaseController extends Controller {

    public function show(){
        $info = I('get.');
        $detail = D('Book')->getBook($info['namespace'], $info['name']);

        $header = getallheaders();
        $return_json = false;
        //判断是json返回， 还是html返回
        if(strpos($header['Accept'], 'application/json') !== false){
            $return_json = true;
        }
        //返回json内容
        $article = null;
        if($info['id'] != 'content'){
            $article = D('BookRelease')->getArticleById($detail['id'], $info['id']);
            $article['path'] = $article['id'];
        }
        
        //ajax请求直接返回json
        if($info['id'] != 'content' && $return_json){
            $this->ajaxReturn($article);
        }

        //页面请求
        if($info['id'] == 'content'){
            //返回当前文章信息
            // $article = D('BookRelease')->getArticleById($detail['id'], $info['id']);
            // var_dump($article_detail);
        }
        // var_dump($header);exit;
        $config = $detail;
        $options = [
            'book' => $detail,
            'plugins' => [
                'host' => 'plugins.kancloud.cn',
                'scheme' => 'https'
            ],
            'web' => [
                'host' => 'www.kancloud.cn',
                'scheme' => 'https'
            ],
            'environment'=>'reader',
            'base' => "/{$detail['namespace']}/{$detail['name']}/content",
            'entry' => "/{$detail['namespace']}/{$detail['name']}"

        ];
        // 获取目录
        $summary = D('BookRelease')->getBookSummary($detail['id']);
        $book['config'] = $config;
        $book['options'] = $options;
        $book['style'] = '';
        $book['article'] = $article;
        $book['summary'] = $summary;

        // var_dump($book);exit;

        $this->assign('book_json', json_encode($book));
        $this->display();
    }
}